﻿using UnityEngine;
using System.Collections;
using MenuItems;
using KeyBindings;

namespace GUIElements
{
	/// <summary>
	/// Menu active event.
	/// </summary>
	public delegate void MenuActiveEvent (bool active);

	/// <summary>
	/// Menu for game functions.
	/// </summary>
	public class Menu : MonoBehaviour
	{
		//Class Variables
		private static Menu m_Instance;
		private bool visible;
		private Rect windowRect;
		public event MenuActiveEvent MenuStateChange;
		
		//Public variables
		public GUIStyle WindowStyle;
		public GUIStyle boxStyle;
		public GUIStyle buttonStyle;
		public GUISkin Skin;

		void Awake ()
		{
			float height = WindowStyle.active.background.height;
			float width = WindowStyle.active.background.width;
			windowRect = new Rect (Screen.width / 2 - width / 2, Screen.height / 2 - height / 2, width, height);
			visible = false;
			m_Instance = this;
		}

		void Update ()
		{
		
		}

		void OnGUI ()
		{
			GUI.skin = Skin;
			if (Application.loadedLevelName != "battlefield") {
				if (GUI.Button (new Rect (Screen.width - 150, 0, 150, 64), "Menu", buttonStyle)) {
					Visible = true;
				}
			}
			if (KeyManager.Instance ["Pause"]) {
				Visible = true;
			}
			if (visible) {
				GUI.Box (new Rect (0, 0, Screen.width, Screen.height), "", boxStyle);
				GUI.Window (10, windowRect, windowContents, "", WindowStyle);
			}
		}

		/// <summary>
		/// Contains the contents for the window.
		/// </summary>
		/// <param name="WindowID">Window I.</param>
		private void windowContents (int WindowID)
		{
			Rect buttonPos = new Rect (10, 10, 100, 32);
			if (GUI.Button (buttonPos, "Resume")) {
				Visible = false;
			}
			buttonPos.y += 31;
			if (GUI.Button (buttonPos, "Save Game")) {
				
			}
			buttonPos.y += 31;
			if (GUI.Button (buttonPos, "Load Game")) {
				
			}
			buttonPos.y += 31;
			if (GUI.Button (buttonPos, "Options")) {
				Options.Instance.Visible = true;
			}
			buttonPos.y += 31;
			if (GUI.Button (buttonPos, "Main Menu")) {
				Application.LoadLevel ("MainMenu");
			}
			buttonPos.y += 31;
			if (GUI.Button (buttonPos, "Quit")) {
				Application.Quit ();
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="GUIElements.Menu"/> is visible.
		/// </summary>
		/// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
		public bool Visible {
			get {
				return visible;
			}
			set {
				if (MenuStateChange != null) {
					MenuStateChange (value);
				}
				visible = value;
			}
		}

		/// <summary>
		/// Gets the menu instance.
		/// </summary>
		/// <value>The instance.</value>
		public static Menu Instance {
			get {
				if (m_Instance == null) {
					m_Instance = new Menu ();
				}
				return m_Instance; 
			} 
		}
	}

}